#ifndef HISTORYRECORDITEM_H
#define HISTORYRECORDITEM_H

#include <QWidget>
#include "search/SearchDef.h"
#include "sql/sqlite/historydao.h"

namespace Ui {
class HistoryRecordItem;
}

class HistoryRecordItem : public QWidget
{
    Q_OBJECT
public:
    explicit HistoryRecordItem(QWidget *parent = nullptr,
                               HistoryItemInfo info = {});
    ~HistoryRecordItem();

    void setIsUploaded(bool isUploaded){
        m_recordFileInfo.isUploaded = isUploaded;
    }
    bool isUploaded() const {return m_recordFileInfo.isUploaded;}
    bool isSelected() const { return m_isSelected; }
    void setSelected(bool selected);
    void setHovered(bool isHovered){m_isHovered = isHovered;}
    HistoryItemInfo getFileInfo() const { return m_recordFileInfo; }
protected:
    void paintEvent(QPaintEvent *event) override;
    void enterEvent(QEnterEvent *event) override;
    void leaveEvent(QEvent *event) override;
signals:
    void selectionChanged(bool selected);

    void attemptToUpload();

    void deleteThisRecord(HistoryRecordItem*);

private slots:
    void on_CheckBox_clicked();

    void on_OpenFileBtn_clicked();

    void on_BackUpBtn_clicked();

    void on_DeleteBtn_clicked();

private:
    Ui::HistoryRecordItem *ui;
    HistoryItemInfo m_recordFileInfo;
    bool m_isSelected = false;
    bool m_isHovered = false;

    QString formatFileSize(quint64 bytes) {
        constexpr quint64 KB = 1024;
        constexpr quint64 MB = KB * 1024;
        constexpr quint64 GB = MB * 1024;

        if (bytes >= GB) {
            return QString("%1 GB").arg(QString::number(bytes / (double)GB, 'f', 2));
        } else if (bytes >= MB) {
            return QString("%1 MB").arg(QString::number(bytes / (double)MB, 'f', 2));
        } else if (bytes >= KB) {
            return QString("%1 KB").arg(QString::number(bytes / (double)KB, 'f', 2));
        } else {
            return QString("%1 B").arg(bytes);
        }
    }
};

#endif // HISTORYRECORDITEM_H
